﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;
using Microsoft.Phone.Controls.Maps.Core;
using G.Navigate;

namespace FY13.View
{
    public partial class MapPage : GBasePage
    {
        /// <summary>
        /// 偏差距离
        /// </summary>
        private readonly Point OffsetPoint = new Point(-0.00402879552734703, 0.00209359227262951);
        public MapPage()
        {
            InitializeComponent();

            UriBuilder tileSourceUri = new UriBuilder("http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41");
            //MapTileLayer tileLayer = new MapTileLayer(); //初始化一个图层
            LocationRectTileSource tileSource = new LocationRectTileSource(
            tileSourceUri.Uri.ToString(),
            null,
            new Range<double>(1, 17.8));  //初始化LocationRectTileSource对象，设定显示范围及放大级别
            myMapLayer.TileSources.Add(tileSource); //指定图层的TileSource
            this.myMap.Mode = new MercatorMode();
        }
        System.Device.Location.GeoCoordinateWatcher wa;
        void btnLocation_Click(object sender, EventArgs e)
        {
            if (wa == null)
            {
                wa = new System.Device.Location.GeoCoordinateWatcher();
                wa.PositionChanged += new EventHandler<System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>>(wa_PositionChanged);
                wa.StatusChanged += new EventHandler<System.Device.Location.GeoPositionStatusChangedEventArgs>(wa_StatusChanged);
            }

            Microsoft.Phone.Shell.ProgressIndicator p = Microsoft.Phone.Shell.SystemTray.GetProgressIndicator(this);
            if (p == null)
            {
                p = new ProgressIndicator();
                Microsoft.Phone.Shell.SystemTray.SetProgressIndicator(this, p);
            }
            p.IsIndeterminate = true;
            p.Text = "正在定位...";
            p.IsVisible = true;
            wa.Start();
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            myMap.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void UnregisterLoading()
        {
            Microsoft.Phone.Shell.ProgressIndicator p = Microsoft.Phone.Shell.SystemTray.GetProgressIndicator(this);
            if (p != null)
            {
                p.IsIndeterminate = false;
                p.IsVisible = false;
            }
        }
        Pushpin myLocationPushpin;
        void wa_PositionChanged(object sender, System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> e)
        {
            //var myLocationPushpin = myMap.FindName("myLocationPushpin") as Pushpin;
            if (myLocationPushpin == null)
            {
                myLocationPushpin = new Pushpin() { Content = "当前" };
                myMap.Children.Add(myLocationPushpin);
            }

            myLocationPushpin.Location = GeoOffsetFixed(e.Position.Location);
            myLocationPushpin.Visibility = System.Windows.Visibility.Visible;
        }
        /// <summary>
        /// 坐标偏差修正
        /// </summary>
        /// <returns></returns>
        private GeoCoordinate GeoOffsetFixed(GeoCoordinate geo)
        {
            geo.Longitude -= OffsetPoint.X;
            geo.Latitude -= OffsetPoint.Y;
            return geo;
        }
        void wa_StatusChanged(object sender, System.Device.Location.GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    MessageBox.Show("请在手机的设置中启用GPS服务");
                    this.UnregisterLoading();
                    break;
                case GeoPositionStatus.NoData:
                    MessageBox.Show("GPS获取数据失败");
                    this.UnregisterLoading();
                    break;
                case GeoPositionStatus.Ready:
                    myMap.Center = GeoOffsetFixed(wa.Position.Location);
                    this.UnregisterLoading();
                    break;
            }

            //e.Status= GeoPositionStatus.
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.GoBack();
        }

        private void btnMy_Click(object sender, EventArgs e)
        {
            GloabalData.GoUserInfo();
        }

        private void myMap_ViewChangeOnFrame(object sender, MapEventArgs e)
        {

        }

        private void btnFull_Click(object sender, EventArgs e)
        {
            if (Grid_full.Visibility == System.Windows.Visibility.Collapsed)
            {
                ContentPanel.Children.Remove(myMap);
                Grid_full.Children.Add(myMap);
                Grid_full.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                Grid_full.Children.Remove(myMap);
                ContentPanel.Children.Add(myMap);
             
                Grid_full.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}